package com.tedu.ivos.user.service.impl;


import com.tedu.ivos.base.exception.ServiceException;
import com.tedu.ivos.base.response.StatusCode;
import com.tedu.ivos.user.mapper.UserMapper;
import com.tedu.ivos.user.pojo.dto.UserLoginParam;
import com.tedu.ivos.user.pojo.dto.UserQuery;
import com.tedu.ivos.user.pojo.dto.UserSaveParam;
import com.tedu.ivos.user.pojo.entity.User;
import com.tedu.ivos.user.pojo.vo.UserVO;
import com.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    /*使用快捷键 Alt + insert 选择implements Methos*/

    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务，参数：{}",userLoginParam);
        // 根据前端传来的数据调用userMapper的selectByUsername方法 Alt+enter 在userMapper中生成方法
        UserVO userVO =userMapper.selectByUsername(userLoginParam.getUsername());
        // 根据前端传来的数据判断用户是否存在
        if (userVO == null){
            // 抛出自定义业务层异常 账号不存在 StatusCode.USERNAME_ERROR
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            // 抛出自定义业务层异常 密码错误 StatusCode.PASSWORD_ERROR
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //如果没有抛出异常，业务将会正常处理完毕的userVO结果返回给Controller层
        // 再由Controller层返回给前端
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存业务，参数：{}",userSaveParam);
        User user=new User();
        BeanUtils.copyProperties(userSaveParam,user);
        // 判断是修改还是新增
        if (user.getId()==null){
            user.setPassword("123456");//设置员工密码
            user.setCreateTime(new Date());// 设置创建时间
            userMapper.insert(user);//调用mapper的insert方法
        }else {
            // TODO 修改
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {

        List<UserVO> list=userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        User user=new User();
        user.setId(userId);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("修改状态业务，参数：{},{}",userId,status);
        User user=new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除业务，参数：{}",userId);
        userMapper.deleteById(userId);
    }
}
